/**
 * @class MiPiace
 * @param divId
 * @param username
 * @param owner
 * @param id
 * @param controller
 * @param readAction
 * @param writeAction
 * @returns
 */
function MiPiace (divId,username,owner,id,controller,readAction,writeAction) {
	
	/**
	 * @field
	 */
	var that = this;
	/**
	 * @field
	 */
	this.like = undefined;
	/**
	 * @field
	 */
	this.username = username;
	/**
	 * @field
	 */
	this.owner = owner;
	/**
	 * @field
	 */
	this.id = id;
	/**
	 * @field
	 */
	this.controller = controller;
	/**
	 * @field
	 */
	this.readAction = readAction;
	/**
	 * @field
	 */
	this.modifyAction = 'modifyAction';
	/**
	 * @field
	 */
	this.writeAction = writeAction;
	/**
	 * @field
	 */
	this.mipiace = $('#' + divId);
	/**
	 * @field
	 */
	this.mipiaceLike = $('#' + divId + ' .Like textarea');
	/**
	 * writeLike
	 * @param writelike
	 */
	this.writeLike = function(valorelike) {
		
		if(window.console && window.debug){
			window.console.info('Like setting ...');
		}
		
		var data = {
				c : that.controller,
				a : that.writeAction,
				u : 'on',
				like : valorelike,
				idfoto: that.id
		}

		$.ajax({
			type:'POST',
			url:'index.php',
			data:data,
			success:that.loadLike,
			error:that.mostraErrore
		});
		
	}
	/**
	 * LoadLike
	 */
	this.loadLike = function () {
		
		if(window.console && window.debug){
			window.console.info('Like loading ...');
		}
		
		//post data
		var data = {
				c : that.controller,
				a : that.readAction,
				id: that.id,
				u : 'on'
				
		} 
		
		$.ajax({
			type:'POST',
			url:'index.php',
			data: data,
			success:that.lbSuccess,
			error:that.mostraErrore
		});
	}
	/**
	 * lbSuccess
	 * @param jsonArray
	 */
	this.lbSuccess = function(jsonArray){
		//TODO: parsing dei like invece che dei likes
		that.likes = $.parseJSON(jsonArray);
		if(window.console && window.debug){
			window.console.info('Likes loaded!');
		}
		that.createLike();
		
	}
	/**
	 * createLike
	 */

	this.createLike =  function(){
		
		if(window.console && window.debug){
			window.console.info('mipiace loaded!');
		}
		
		that.mipiace.html('');
		
		//Vedo se e' vuoto
		var empty = true;
		
		for(var i in that.likes){
			empty = false;
		}
		
		if(that.likes == undefined || empty){
			
			var spazio1 = $('<div class="spazio1"></div>');
			var linkmipiace = $('<a href="#" >Mi Piace</a> ').button({
				icons:{
					primary: "ui-icon-heart"
				}
			}).bind('click',
					{num:i},
					function(event){
						that.writeLike(1);
						return false;
			});
			
			spazio1.append(linkmipiace);
			that.mipiace.append(spazio1);
			
			var spazio2 = $('<div class="spazio2">' + '<p>A nessuno piace questa foto!</p>' + '</div>');
			that.mipiace.append(spazio2);
			return false;
			
		}
		
		var num = that.likes.length;
		var likes = that.likes;
		var flag = 0;

		if(num != 0){
			var spazio2 = $('<div class="spazio2">Ai seguenti utenti piace questa foto: </div>');
			for(var i = 0; i < num; i++) {
				if (that.username == that.likes[i].writer) {
					var paragrafomipiace = $('<div>' + that.likes[i].writer + '</div>');
					flag = 1;
				}
			}	
			if (flag == 1) {
				var spazio1 = $('<div class="spazio1"></div>');
				var linkmipiace = $('<a href="#" >Non Mi Piace Pi&ugrave;</a> ').button({
					icons:{
						primary: "ui-icon-heart"
					}
				}).bind('click',
						{num:i},
						function(event){
							that.writeLike(0);
							return false;
				});
					
				spazio1.append(linkmipiace);
				that.mipiace.append(spazio1);

			}
			else {
				var spazio1 = $('<div class="spazio1"></div>');
				var linkmipiace = $('<a href="#" >Mi Piace</a> ').button({
					icons:{
					primary: "ui-icon-heart"
				}
				}).bind('click',
						{num:i},
						function(event){
							that.writeLike(1);
							return false;
				});
					
				spazio1.append(linkmipiace);
				that.mipiace.append(spazio1);

			}
			
			for(var i = 0; i < num; i++) {
				var spazio3 = $('<a href="javascript:ajax.doAjax(\'index.php?c=CUtente&a=mostraProfilo&username=' + that.likes[i].writer + '\')" >' + that.likes[i].writer + '</a>');
				spazio2.append(spazio3);
				if (i < (num-1)) {
					spazio2.append(', ');
				}
			}
			that.mipiace.append(spazio2);
			return false;
		}
	}
	/**
	 * mostraErrore
	 */
	this.mostraErrore = function(){
		
		if(window.console && window.debug){
			window.console.info('Si e\' verificato un errore!');
		}
	
	}
	
	this.loadLike();
	
}


